package com.raising.utils;
import java.lang.reflect.Field;

/**
 * 反射工具类 
 * @author GaoYuan
 * @datetime 2017/12/1 下午2:41
 */
public class ReflectToStringUtil {

    static StringBuffer sb = new StringBuffer();
    /**
     * 打印实体类
     * @author GaoYuan
     * @datetime 2017/12/1 下午2:41
     */
    public static String toStringUtil(Object clazs,boolean isOutputNull) {
        sb.append("\n*********************************************\n");
        getParamAndValue(clazs, clazs.getClass(), isOutputNull);
        sb.append("*********************************************\n");
        return sb.toString();
    }

    private static void getParamAndValue(Object clazs, Class<?> clazz,boolean isOutputNull) {
        Class<?> sc = clazz.getSuperclass();
        Field[] sfields = sc.getDeclaredFields();
        if (sfields.length > 0) {
            getParamAndValue(clazs, sc, isOutputNull);
        }
        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields) {
            f.setAccessible(true);
            try {
                if (null != f.get(clazs)||isOutputNull){
                    sb.append("   " + f.getName() + " = " + f.get(clazs) + "\n");
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

}